package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.user.model.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-09-12 14:21
 */
@RestController
@RequestMapping("/api/user")
public class PassPortController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户登录
     *
     * @param userInfo
     * @return {token:"用户令牌", nickName:"atguigu"}
     */
    @ApiOperation("用户登录")
    @PostMapping("/passport/login")
    public Result login(@RequestBody UserInfo userInfo, HttpServletRequest request) {
        Map<String, Object> mapResult = userInfoService.login(userInfo, request);
        return Result.ok(mapResult);
    }


    /**
     * 用户退出
     *
     * @param token
     * @return
     */
    @ApiOperation("用户退出")
    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader("token") String token) {
        userInfoService.logout(token);
        return Result.ok();
    }
}
